package com.linjiahao.orderservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.linjiahao.commonutils.ResultMap;
import com.linjiahao.orderservice.client.EduClient;
import com.linjiahao.orderservice.entity.Order;
import com.linjiahao.orderservice.service.OrderService;
import com.linjiahao.orderservice.service.PayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-09-08
 */
@RestController
@RequestMapping("/orderservice/pay-log")
public class PayLogController {

    @Autowired
    private PayLogService payService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private EduClient eduClient;

    @GetMapping("/createNative/{orderNo}")
    public ResultMap createNative(@PathVariable String orderNo) {
        Map map = payService.createNative(orderNo);
        return ResultMap.ok().data(map);
    }

    @GetMapping("/queryPayStatus/{orderNo}")
    public ResultMap queryPayStatus(@PathVariable String orderNo) {
        //调用查询接口
        Map<String, String> map = payService.queryPayStatus(orderNo);
        if (map == null) {//出错
            return ResultMap.error().message("支付出错");
        }
        if (map.get("trade_state").equals("SUCCESS")) {//如果成功
            //更改订单状态
            payService.updateOrderStatus(map);
            Order order_no = orderService.getOne(new QueryWrapper<Order>().eq("order_no", orderNo));
            eduClient.updatabuyCount(order_no.getCourseId());
            return ResultMap.ok().message("支付成功");
        }

        return ResultMap.ok().code(25000).message("支付中");
    }

}

